// pages/A/user_order/user_order.js
let api = require('../../../api/api.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: [{
      name: "用户明细"
    }, {
      name: "订单明细"
    }],
    order_navbar: [{
        name: '已支付',
      },
      {
        name: '已完成',
      },
      {
        name: '已发货',
      },
      {
        name: '已关闭',
      },
    ],
    currentTabsIndex: 0, //  用户明细/订单明细 初始值下标
    order_currentTabsIndex: 0, //    已支付/已完成/已发货/已关闭 初始值下标
    hide: true, //  搜索小图标初始值
    isSearch: true, // 没有查到用户
    isUserSearch: true, // 没有查到订单
    
  },
  // 用户明细/订单明细  tab 切换
  navbar: function(e) {
    wx.showLoading({
      title: "加载中"
    })
    setTimeout(function() {
      wx.hideLoading()
    }, 1000)
    console.log(e)
    var index = e.currentTarget.dataset.index
    if (index == 0) {
      var param = {}
      param.id = this.data.data.id
      param.type = this.data.data.type
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      api.users_details(param, res => {
        console.log(res)
        this.setData({
          indexData: res.data.result
        })
      })
    }
    if (index == 1) {
      var content = []
      var param = {}
      param.id = this.data.data.id
      param.mobile = this.data.data.mobile
      param.level = this.data.data.level
      param.type = this.data.data.type
      api.userorder(param, res => {
        console.log(res)
        var result = res.data.result
        var content = []
        if (result.length > 0) {
          result.forEach(function(element) {
            if (element.order_status == '2') {
              element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
              content.push(element)
            }
          })
        }
        this.setData({
          indexData: content,
          result: result
        })
      })
    }
    var index = e.currentTarget.dataset.index
    this.setData({
      currentTabsIndex: index
    })
  },
  // 已支付/已完成/已发货/已关闭  tab 切换
  order_navbar: function(e) {
    console.log(e)
    var index = e.currentTarget.dataset.index
    var result = this.data.result
    result.forEach(function (element) {
      element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
    })
    var content = []
    if (index == 0) {
      console.log("----0----")
      result.forEach(function(element) {
        if (element.order_status == '2') {
          content.push(element)
        }
      })
    } else if (index == 1) {
      console.log("----1----")
      result.forEach(function(element) {
        if (element.order_status == '3') {
          content.push(element)
        }
      })
    } else if (index == 2) {
      console.log("----2----")
      result.forEach(function(element) {
        if (element.order_status == '5') {
          content.push(element)
        }
      })
    } else if (index == 3) {
      console.log("----3----")
      result.forEach(function(element) {
        if (element.order_status == '4') {
          content.push(element)
        }
      })
    }
    this.setData({
      order_currentTabsIndex: index,
      indexData: content
    })
  },
  // input 获取焦点
  foucus: function(e) {
    this.setData({
      hide: false
    })
  },
  // input 失去焦点
  blur: function(e) {
    this.setData({
      hide: true
    })
  },
  // 图标 X 点击事件
  clear: function() {
    var data = this.data.data
    var param = {}
    param.id = data.id
    param.type = data.type
    param.level = data.level
    param.mobile = data.mobile
    api.users_details(param, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result
      })
    })
    this.setData({
      searchValue: "",
      indexData: this.data.result
    })
  },
  clear1: function() {
    var data = this.data.data
    var content = []
    var param = {}
    param.id = data.id
    param.type = data.type
    param.level = data.level
    param.mobile = data.mobile
    api.userorder(param, res => {
      console.log(res)
      var result = res.data.result
      if (result == "") {
        this.setData({
          indexData: [],
          content: []
        })
      } else {
        result.forEach(function(element) {
          
          if (element.order_status == 2) {
            element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
            content.push(element)
            
          }

        })
        this.setData({
          indexData: content,
          content: result
        })
      }
      this.setData({
        currentTabsIndex_1: 0,
        searchValue: "",
        isUserSearch: true
      })
    })
  },
  // 图标 返回 点击事件
  confirm: function(e) {
    var data = this.data.data
    var param = {}
    param.id = data.id
    param.type = data.type
    param.level = data.level
    param.mobile = data.mobile
    api.users_details(param, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result
      })
    })
  },
  confirm1: function() {
    var data = this.data.data
    var content = []
    var param = {}
    param.id = data.id
    param.type = data.type
    param.level = data.level
    param.mobile = data.mobile
    api.userorder(param, res => {
      console.log(res)
      var result = res.data.result
      if (result == "") {
        this.setData({
          indexData: [],
          content: []
        })
      } else {
        result.forEach(function(element) {
          if (element.order_status == 2) {
            element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
            content.push(element)
          }
        })
        this.setData({
          indexData: content,
          content: result
        })
      }
      this.setData({
        currentTabsIndex_1: 0,
        isUserSearch: true
      })
    })
  },
  // 用户详情 input 点击事件
  search: function(e) {
    let value = e.detail.value
    console.log(value)
    let data = this.data.data
    data.name = value
    api.users_details(data, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result,
        isSearch: false
      })
    })
  },
  search1:function(e){
    var value = e.detail.value
    console.log(value)
    var data = this.data.data
    data.name = value
    var content = []
    api.userorder(data, res => {
      console.log(res)
      var result = res.data.result
      if (result == "") {
        this.setData({
          indexData: [],
          content: []
        })
      } else {
        result.forEach(function (element) {
          
          if (element.order_status == 2) {
            element.user_mobile = element.user_mobile.substr(0, 3) + "******" + element.user_mobile.substr(8)
            content.push(element)
          }
        })
        this.setData({
          indexData: content,
          content: result,
          isUserSearch: false
        })
      }
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(data) {
    wx.showLoading({
      title: "加载中"
    })
    setTimeout(function() {
      wx.hideLoading()
    }, 1500)
    api.users_details(data, res => {
      console.log(res)
      this.setData({
        indexData: res.data.result
      })
    })
    this.setData({
      data: data,
      level: app.globalData.userInerInfo.level
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})